package com.meme.editor.property;

import java.util.Hashtable;
import java.util.Vector;

import org.apache.log4j.Logger;

//------------------------------------------------------------
//class PropertyGroup
//------------------------------------------------------------
	
public class PropertyGroup 
{
	
	static Logger logger = Logger.getLogger (PropertyGroup.class);		
	{
		logger.setLevel (org.apache.log4j.Level.ERROR);
	}
	
	PropertyGroupSet mGroupSet;
	
	String mName;
	
	// same as mName now
	String mLabel;
	
	Vector<Property>            mProperties  = new Vector<Property> ();
	Hashtable<String, Property> mLookupTable = new Hashtable<String, Property> ();
	
	protected PropertyGroup (PropertyGroupSet groupSet, String name)
	{
		this (groupSet, name, null);
	}
	
	// only PropertyGroupList can construct a PropertyGroup
	protected PropertyGroup (PropertyGroupSet groupSet, String name, String label)
	{
		mGroupSet = groupSet;
		
		mName = name;
		if (label == null)
			label = name;
		mLabel = label;
	}
	
	protected PropertyGroupSet getGroupSet ()
	{
		return mGroupSet;
	}
	
	protected String getName ()
	{
		return mName;
	}
	
	protected String getLabel ()
	{
		return mLabel;
	}	
	
	protected Property createProperty (String propertyName, String valueType)
	{
		Property oldProperty = mLookupTable.get (propertyName);
		if ( oldProperty != null )
		{
			logger.warn ("Property [" + propertyName + "] already exists. Modify it with the new value.");

			return oldProperty;
		}
		else
		{
			Property newProperty = new Property (this, propertyName, valueType);
			//newProperty.setGroup (this);
			
			mProperties.add (newProperty);
			mLookupTable.put (newProperty.getName(), newProperty);	
			
			return newProperty;
		}
	}

	
	protected Property removeProperty (String name)
	{
		Property property = getProperty (name);
		
		if ( property != null )
		{
			mLookupTable.remove (name);
			mProperties.remove (property);
			
			property.setGroup (null);
		}

		return property;
	}
	
	protected Property getProperty (String name)
	{
		return getProperty (name, true);	
	}
	
	protected Property getProperty (String name, boolean showWarning)
	{
		Property property = mLookupTable.get (name);
		
		if ( property == null && showWarning )
			logger.warn ("Property [" + name + "] doesn't exist in Group [" + getName () + "].");
		
		return property;	
	}
	
	protected int getPropertiesCount ()
	{
		return mProperties.size ();
	}
	
	protected Property getProperty (int i)
	{
		logger.assertLog ( mProperties.size() > i && i >= 0, "Out of boundary." );
		
		return mProperties.elementAt (i);
	}
	
	protected Property removeProperty (int i)
	{
		logger.assertLog ( mProperties.size() > i && i >= 0, "Out of boundary." );
		
		Property property = mProperties.remove (i);
		
		if ( property != null )
		{
			mLookupTable.remove (property.getName());
			
		}		
		
		return property;
	}
	
	
//---------------------------------------------------------------------------
	
	// for debug
	protected void dump ()
	{
		logger.debug ("PropertyGroup: name=" + mName);
		for ( int i = 0; i < mProperties.size(); ++ i )
		{
			mProperties.elementAt (i).dump ();			
		}
	}	
}	
